package khleuven.mondzorg.app;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import khleuven.mondzorg.app.SimpleGestureFilter.SwipeGestureListener;
import khleuven.mondzorg.domain.Learn;
import khleuven.mondzorg.domain.Module;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class LearnActivity extends ActivityWithIllustrations implements SwipeGestureListener{

	private Module module;
	private int sequence = 1;
	private Learn learn;
	
	private SimpleGestureFilter detector;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_learn);
		fetchIntentExtras();
		setTitle();
		setLearn();		

		detector = new SimpleGestureFilter(this, this);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent me){
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}
	
	private void fetchIntentExtras(){
		Logger.logApp("aangekomen in fetchIntentExtras Learn");
		Bundle b = getIntent().getExtras();
		if(b!=null){
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null");
			module = new Module(b.getString(ModulesActivity.MODULE_NAME));
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null module: "+module.getName());
		}
	}
	
	@Override
	protected void onResume(){
		super.onResume();
	}	
	
	private void setTitle(){
		TextView tvmodule = (TextView)findViewById(R.id.txt_module);
		tvmodule.setText(module.getName());
	}

	private boolean setLearn(){
		boolean found = false;
		Logger.logApp("sequence: "+sequence);
		learn = dentalcare.getPreferredLearn(module,sequence);    
		TextView tvseq = (TextView)findViewById(R.id.txt_sequence);
		TextView tvlesson = (TextView)findViewById(R.id.txt_lesson);
		if(learn != null){
			Logger.logApp("learn gevonden");
	        String lesson = dentalcare.getLearnLesson(learn);	        
	        setTextViewHTML(tvlesson, lesson);
	        tvlesson.setMovementMethod(LinkMovementMethod.getInstance());			
			tvseq.setText(getString(R.string.lesson)+" "+sequence);
	        setIllustration();
	        found = true;
		} else {
			Logger.logApp("learn niet gevonden");
			tvseq.setText(getString(R.string.lessonsup));
			tvlesson.setText(getString(R.string.lessonscomplete));
			setIllustration();
		}
		return found;
	}
	
	protected void setIllustration(){		
		illustrations = dentalcare.getLearnIllustrations(learn);
		super.setIllustration();
	}
	
	protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
	{
	    ClickableSpan clickable = new ClickableSpan() {
	          public void onClick(View view) {	        	  
	              String defString = span.getURL();
	              Logger.logApp("NU MOETEN WE NAAR DEF: "+defString);
	      		  Intent intent = new Intent(LearnActivity.this, DefinitionsActivity.class);
	      		  intent.putExtra(DefinitionsActivity.DEF_KEY, defString);
	      		  startActivity(intent);	
	          }
	    };
	    super.makeLinkClickable(strBuilder, span, clickable);
	}


	@Override
	public void onSwipe(int direction) {
		if(direction == SimpleGestureFilter.SWIPE_RIGHT){
			Logger.logApp("swipe right --> next");
			if(sequence > 1){
				sequence --;
			}
		} else{
			Logger.logApp("swipe left --> previous");
			if(learn != null){
				sequence ++;	
			}
		}	
		setLearn();		
	}
}